<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分类管理 - 学生知识管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .nav {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 2rem;
        }
        
        .logo {
            font-size: 1.5rem;
            font-weight: bold;
        }
        
        .nav-links {
            display: flex;
            list-style: none;
            gap: 2rem;
        }
        
        .nav-links a {
            color: white;
            text-decoration: none;
            transition: opacity 0.3s;
        }
        
        .nav-links a:hover {
            opacity: 0.8;
        }
        
        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        
        .page-header {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .page-title {
            font-size: 1.8rem;
            color: #333;
        }
        
        .btn {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            transition: background 0.3s;
        }
        
        .btn-primary {
            background: #667eea;
            color: white;
        }
        
        .btn-primary:hover {
            background: #5a6fd8;
        }
        
        .btn-secondary {
            background: #6c757d;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .btn-danger:hover {
            background: #c82333;
        }
        
        .btn-sm {
            padding: 0.5rem 1rem;
            font-size: 0.875rem;
        }
        
        .categories-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 1.5rem;
        }
        
        .category-card {
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
            transition: transform 0.3s, box-shadow 0.3s;
        }
        
        .category-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(0,0,0,0.15);
        }
        
        .category-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1.5rem;
            text-align: center;
        }
        
        .category-icon {
            font-size: 2rem;
            margin-bottom: 0.5rem;
        }
        
        .category-name {
            font-size: 1.25rem;
            font-weight: 600;
            margin-bottom: 0.5rem;
        }
        
        .category-description {
            font-size: 0.875rem;
            opacity: 0.9;
        }
        
        .category-content {
            padding: 1.5rem;
        }
        
        .category-stats {
            display: flex;
            justify-content: space-between;
            margin-bottom: 1rem;
            font-size: 0.875rem;
            color: #666;
        }
        
        .stat-item {
            display: flex;
            align-items: center;
            gap: 0.25rem;
        }
        
        .category-actions {
            display: flex;
            gap: 0.5rem;
            justify-content: center;
        }
        
        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
        }
        
        .modal-content {
            background-color: white;
            margin: 5% auto;
            padding: 2rem;
            border-radius: 10px;
            width: 90%;
            max-width: 500px;
            position: relative;
        }
        
        .close {
            position: absolute;
            right: 1rem;
            top: 1rem;
            font-size: 1.5rem;
            cursor: pointer;
            color: #666;
        }
        
        .close:hover {
            color: #333;
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: #333;
        }
        
        .form-group input,
        .form-group textarea,
        .form-group select {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 1rem;
            font-family: inherit;
        }
        
        .form-group input:focus,
        .form-group textarea:focus,
        .form-group select:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        }
        
        .form-group textarea {
            min-height: 100px;
            resize: vertical;
        }
        
        .form-actions {
            display: flex;
            gap: 1rem;
            justify-content: flex-end;
            margin-top: 2rem;
        }
        
        .alert {
            padding: 1rem;
            border-radius: 5px;
            margin-bottom: 1rem;
        }
        
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .empty-state {
            text-align: center;
            padding: 3rem;
            color: #666;
        }
        
        .empty-state h3 {
            margin-bottom: 1rem;
            color: #333;
        }
        
        @media (max-width: 768px) {
            .page-header {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }
            
            .categories-grid {
                grid-template-columns: 1fr;
            }
            
            .category-actions {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <nav class="nav">
            <div class="logo">🎓 学生知识管理系统</div>
            <ul class="nav-links">
                <li><a href="/">首页</a></li>
                <li><a href="/articles">文章</a></li>
                <li><a href="/categories">分类</a></li>
                <li><a href="/profile">个人中心</a></li>
                <li><a href="/logout">退出</a></li>
            </ul>
        </nav>
    </header>
    
    <div class="container">
        <div class="page-header">
            <h1 class="page-title">分类管理</h1>
            <button onclick="openModal('createModal')" class="btn btn-primary">📁 新建分类</button>
        </div>
        
        <div th:if="${message}" class="alert alert-success" th:text="${message}">操作成功</div>
        <div th:if="${error}" class="alert alert-error" th:text="${error}">操作失败</div>
        
        <div class="categories-grid" th:if="${categories != null and !categories.empty}">
            <div class="category-card" th:each="category : ${categories}">
                <div class="category-header">
                    <div class="category-icon" th:text="${category.icon != null ? category.icon : '📁'}">📁</div>
                    <div class="category-name" th:text="${category.name}">分类名称</div>
                    <div class="category-description" th:text="${category.description}">分类描述</div>
                </div>
                
                <div class="category-content">
                    <div class="category-stats">
                        <span class="stat-item">
                            📄 <span th:text="${category.articleCount != null ? category.articleCount : 0}">0</span> 篇文章
                        </span>
                        <span class="stat-item" th:if="${category.enabled}">
                            ✅ 启用
                        </span>
                        <span class="stat-item" th:unless="${category.enabled}">
                            ❌ 禁用
                        </span>
                    </div>
                    
                    <div class="category-actions">
                        <a th:href="@{/articles(categoryId=${category.id})}" class="btn btn-secondary btn-sm">查看文章</a>
                        <button th:onclick="'editCategory(' + ${category.id} + ')'" class="btn btn-primary btn-sm">编辑</button>
                        <button th:onclick="'deleteCategory(' + ${category.id} + ')'" class="btn btn-danger btn-sm">删除</button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="empty-state" th:if="${categories == null or categories.empty}">
            <h3>📁 还没有分类</h3>
            <p>创建你的第一个分类来组织文章吧！</p>
            <button onclick="openModal('createModal')" class="btn btn-primary">立即创建</button>
        </div>
    </div>
    
    <!-- 创建分类模态框 -->
    <div id="createModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('createModal')">&times;</span>
            <h2>创建新分类</h2>
            <form action="/categories/create" method="post">
                <div class="form-group">
                    <label for="createName">分类名称 *</label>
                    <input type="text" id="createName" name="name" placeholder="请输入分类名称" required>
                </div>
                
                <div class="form-group">
                    <label for="createIcon">分类图标</label>
                    <select id="createIcon" name="icon">
                        <option value="📁">📁 文件夹</option>
                        <option value="📚">📚 书籍</option>
                        <option value="💻">💻 编程</option>
                        <option value="🔬">🔬 科学</option>
                        <option value="🎨">🎨 艺术</option>
                        <option value="📊">📊 数据</option>
                        <option value="🌐">🌐 网络</option>
                        <option value="🎯">🎯 目标</option>
                        <option value="⚡">⚡ 技术</option>
                        <option value="🎓">🎓 学习</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="createDescription">分类描述</label>
                    <textarea id="createDescription" name="description" placeholder="请输入分类描述（可选）"></textarea>
                </div>
                
                <div class="form-group">
                    <label for="createEnabled">状态</label>
                    <select id="createEnabled" name="enabled">
                        <option value="true">启用</option>
                        <option value="false">禁用</option>
                    </select>
                </div>
                
                <div class="form-actions">
                    <button type="button" onclick="closeModal('createModal')" class="btn btn-secondary">取消</button>
                    <button type="submit" class="btn btn-primary">创建</button>
                </div>
            </form>
        </div>
    </div>
    
    <!-- 编辑分类模态框 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeModal('editModal')">&times;</span>
            <h2>编辑分类</h2>
            <form id="editForm" method="post">
                <input type="hidden" id="editId" name="id">
                
                <div class="form-group">
                    <label for="editName">分类名称 *</label>
                    <input type="text" id="editName" name="name" placeholder="请输入分类名称" required>
                </div>
                
                <div class="form-group">
                    <label for="editIcon">分类图标</label>
                    <select id="editIcon" name="icon">
                        <option value="📁">📁 文件夹</option>
                        <option value="📚">📚 书籍</option>
                        <option value="💻">💻 编程</option>
                        <option value="🔬">🔬 科学</option>
                        <option value="🎨">🎨 艺术</option>
                        <option value="📊">📊 数据</option>
                        <option value="🌐">🌐 网络</option>
                        <option value="🎯">🎯 目标</option>
                        <option value="⚡">⚡ 技术</option>
                        <option value="🎓">🎓 学习</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label for="editDescription">分类描述</label>
                    <textarea id="editDescription" name="description" placeholder="请输入分类描述（可选）"></textarea>
                </div>
                
                <div class="form-group">
                    <label for="editEnabled">状态</label>
                    <select id="editEnabled" name="enabled">
                        <option value="true">启用</option>
                        <option value="false">禁用</option>
                    </select>
                </div>
                
                <div class="form-actions">
                    <button type="button" onclick="closeModal('editModal')" class="btn btn-secondary">取消</button>
                    <button type="submit" class="btn btn-primary">保存</button>
                </div>
            </form>
        </div>
    </div>
    
    <script>
        function openModal(modalId) {
            document.getElementById(modalId).style.display = 'block';
        }
        
        function closeModal(modalId) {
            document.getElementById(modalId).style.display = 'none';
        }
        
        function editCategory(categoryId) {
            // 这里应该通过AJAX获取分类详情，简化处理
            fetch(`/categories/${categoryId}/details`)
                .then(response => response.json())
                .then(category => {
                    document.getElementById('editId').value = category.id;
                    document.getElementById('editName').value = category.name;
                    document.getElementById('editIcon').value = category.icon || '📁';
                    document.getElementById('editDescription').value = category.description || '';
                    document.getElementById('editEnabled').value = category.enabled.toString();
                    document.getElementById('editForm').action = `/categories/${categoryId}/update`;
                    openModal('editModal');
                })
                .catch(error => {
                    alert('获取分类信息失败：' + error.message);
                });
        }
        
        function deleteCategory(categoryId) {
            if (confirm('确定要删除这个分类吗？删除后该分类下的文章将变为未分类状态。')) {
                window.location.href = `/categories/${categoryId}/delete`;
            }
        }
        
        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modals = document.querySelectorAll('.modal');
            modals.forEach(modal => {
                if (event.target === modal) {
                    modal.style.display = 'none';
                }
            });
        }
    </script>
</body>
</html>